﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageFAQs : Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_FAQ = "__FAQ::FAQ";
    private const string PROP_LAYOUT = "__FAQ::UIL";
    private const string PROP_SELECTED_FAQ_ID = "__FAQ::SFAQID";

    #endregion

    #region Public Properties

    public FAQCollection DataSourceFAQ
    {
        get { return (SessionMgr.Session[PROP_DATASRC_FAQ] is FAQCollection ? (FAQCollection)SessionMgr.Session[PROP_DATASRC_FAQ] : (FAQCollection)(SessionMgr.Session[PROP_DATASRC_FAQ] = new FAQCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_FAQ] = value; }
    }

    public int SelectedFAQID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_FAQ_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_FAQ_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_FAQ_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceFAQ = FAQ.GetAllFAQ();
            FAQList1.DataBind(DataSourceFAQ);
        }
    }

    #endregion

    #region Control: FAQList

    protected void ucFAQList_EditRequested(object sender, Controls_FAQList.EditRequestedEventArgs evt)
    {
        SelectedFAQID = evt.FQ.FAQID;
        FAQViewEdit1.DataBind(evt.FQ);
        SetLayout(LayoutType.Edit);
        divexistname.Visible = false;
    }


    protected void ucFAQList_AddNewRequested(object sender, EventArgs evt)
    {
        FAQViewEdit1.CancelEdit();
        FAQ newFAQ = FAQ.CreateNew(DataSourceFAQ);
        SelectedFAQID = newFAQ.FAQID;
        FAQViewEdit1.DataBind(DataSourceFAQ.FindByFAQID(SelectedFAQID));
        SetLayout(LayoutType.Edit);
        divexistname.Visible = false;
    }


    protected void ucFAQList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceFAQ = FAQ.GetAllFAQ();
        FAQList1.DataSource = DataSourceFAQ;
    }

    #endregion

    #region Control: FAQViewEdit

    protected void ucFAQViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        FAQViewEdit1.DataSourceFAQ = DataSourceFAQ.FindByFAQID(SelectedFAQID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        FAQViewEdit1.CancelEdit();
        // If we were adding a new jobcategory...then remove the incomplete/not added new jobcategory object from the collection.
        if (DataSourceFAQ.FindByFAQID(SelectedFAQID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceFAQ.Remove(DataSourceFAQ.FindByFAQID(SelectedFAQID));
                SelectedFAQID = (DataSourceFAQ[0] != null ? DataSourceFAQ[0].FAQID : 0);
            }
            catch
            {
            }
        }
        FAQList1.DataBind(DataSourceFAQ);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (FAQViewEdit1.EndEdit())
        {
            FAQ obj = DataSourceFAQ.FindByFAQID(SelectedFAQID);
            FAQ.Update(obj);

            // TODO: Set Feedback text...

            FAQList1.DataBind(DataSourceFAQ);
            SetLayout(LayoutType.View);
            divexistname.Visible = false;
        }
        else
        {
            divexistname.Visible = true;
        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlFAQList.Visible = false;
                pnlFAQViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlFAQList.Visible = true;
                pnlFAQViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion


}
